﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Microsoft.Practices.Unity;

namespace Vit.Common
{
    public class HttpContextRequestLifetimeManager : LifetimeManager
    {
        private readonly string key;

        public HttpContextRequestLifetimeManager()
        {
            key = Guid.NewGuid().ToString();
        }

        public override object GetValue()
        {
            if(HttpContext.Current != null)
                return HttpContext.Current.Items[key];

            return null;
        }

        public override void RemoveValue()
        {
            if (HttpContext.Current != null)
                HttpContext.Current.Items.Remove(key);
        }

        public override void SetValue(object newValue)
        {
            if (HttpContext.Current != null)
                HttpContext.Current.Items[key] = newValue;
        }
    }
}
